﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.IO;
using System.Windows.Markup;

namespace UltimateNavigationFramework.Loaders
{
    [ContentProperty("Items")]
    public class ViewCatalog : IViewCatalog
    {
        Collection<IViewInfo> _viewCollection;

        public ViewCatalog()
        {
            this._viewCollection = new Collection<IViewInfo>();            
        }

        public ViewCatalog(IEnumerable<IViewInfo> views)
            : this()
        {
            if (views == null) throw new System.ArgumentNullException("views");
            foreach (var viewInfo in views)
            {
                this.Items.Add(viewInfo);
            }
        }

        public Collection<IViewInfo> Items
        {
            get { return this._viewCollection; }
        }

        public static ViewCatalog CreateFromXaml(Stream xamlStream)
        {
            string xaml;
            using (var reader = new System.IO.StreamReader(xamlStream))
            {
                xaml = reader.ReadToEnd();
            }
            return XamlReader.Load(xaml) as ViewCatalog;
        }

        public static ViewCatalog CreateFromXaml(Uri uri)
        {
            var streamInfo = System.Windows.Application.GetResourceStream(uri);

            if ((streamInfo != null) && (streamInfo.Stream != null))
            {
                return CreateFromXaml(streamInfo.Stream);
            }

            return null;
        }    
    }
}
